package de.fuberlin.inf.alp2;

import de.fuberlin.inf.alp2.utils.*;

/**
 * This class has been implemented within the scope of the forth exercise of
 * the lecture <i>"Algorithmen und Programmierung 2 - Objektorientierte
 * Programmierung"</i>.
 * @author stefanr
 */
public class Rectangular {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final boolean ISRECTANGULAR;
		ISRECTANGULAR = Rectangular.isRectangular(Rectangular.getParams(args));
		System.out.println(ISRECTANGULAR ? "Ja" : "Nein");
	}
	
	private static boolean isRectangular(double[] p) {
		Quicksort.sort(p);
		return (p[0] * p[0] + p[1] * p[1] == p[2] * p[2]);
	}


	/**
	 * This method will use the first three parameters if given. Otherwise it
	 * requests inputs by the user, until the entered strings has been
	 * successfully converted to double.
	 * @param args If the first three arguments contains doubles, they will be
	 * used as lengths of sides. Otherwise the program will ask you to enter the
	 * values again.
	 * @return An array with three doubles (a, b and c).
	 */
	private static double[] getParams(String[] args) {
		double[] parameters = new double[3];
		
		if (args.length > 1) {
			try {
				for (int i = 0; i < 3; i++)
					parameters[i] = Double.parseDouble(args[i]);
			}
			catch (NumberFormatException exc) { }
		} // args.length > 0
		for (int i = 0; i < 3; i++)
			while (parameters[i] <= 0)
				parameters[i] = ConsoleInput.readDouble("Please enter length of"
						+ " " + (i + 1) + ". side of triangle.", "Entered value"
						+ " is invalid.");
		
		return parameters;
	} // getParameters

}
